/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.TblAccountCp;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author duongnv
 */
@Stateless
public class ScratchAccountFacade extends AbstractFacade<TblAccountCp> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    public ScratchAccountFacade() {
        super(TblAccountCp.class);
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TblAccountCp update(String userId, String userPass, String ip, String cpName, String cpAddress, String cpPhone, int lockValue, int numConcurrentTrans, int rateLevel, double specialBonus) {
        TblAccountCp tbl = new TblAccountCp();
        try {
            tbl.setUserPass(userPass);
            tbl.setCpAddress(cpAddress);
            tbl.setCpName(cpName);
            tbl.setCpPhone(cpPhone);
            tbl.setIsLocked(lockValue);
            tbl.setIp(ip);
            tbl.setRateLevel(rateLevel);
            tbl.setNumConcurrentTrans(numConcurrentTrans);
            tbl.setSpecialBonus(specialBonus);
            tbl.setLastUpdatedTime(new Date());
            TblAccountCp result = findByUserId(userId);
            if (result == null) {
                em.persist(tbl);
                result = tbl;
            } else {
                tbl.setId(result.getId());
                tbl.setUserId(userId);
                result = em.merge(tbl);
            }
            return result;
        } catch (Exception ex) {
            Logger.getLogger(ScratchAccountFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public TblAccountCp getUserIdByIpName(String ip, String cpName) {
        try {
            TypedQuery<TblAccountCp> q = em.createQuery("SELECT t FROM TblAccountCp t WHERE t.ip = :ip AND t.cpName = :cpName", TblAccountCp.class);
            q.setParameter("ip", ip);
            q.setParameter("cpName", cpName);
            TblAccountCp result = q.getSingleResult();
            return result;
        } catch (Exception ex) {
            Logger.getLogger(KeoChauAFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public TblAccountCp findByUserId(String uId) {
        try {
            TypedQuery<TblAccountCp> q = em.createNamedQuery("TblAccountCp.findByUserId", TblAccountCp.class);
            q.setParameter("userId", uId);
            TblAccountCp result = q.getSingleResult();
            return result;
        } catch (Exception ex) {
            Logger.getLogger(KeoChauAFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public void delete(TblAccountCp acc) throws Exception {
        try {
            super.remove(acc);
        } catch (Exception ex) {
            Logger.getLogger(KeoChauAFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<TblAccountCp> findAll1() {
        try {
            TypedQuery<TblAccountCp> q = em.createNamedQuery("TblAccountCp.findAll", TblAccountCp.class);
            q.setHint("eclipselink.refresh", "true");
            List<TblAccountCp> result = q.getResultList();
            return result;
        } catch (Exception ex) {
            Logger.getLogger(KeoChauAFacade.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
